1 /*
2 Copyright: Marcelo S. N. Mancini (Hipreme|MrcSnm), 2018 - 2021
3 License:   [https://creativecommons.org/licenses/by/4.0/|CC BY-4.0 License].
4 Authors: Marcelo S. N. Mancini
5 
6 	Copyright Marcelo S. N. Mancini 2018 - 2021.
7 Distributed under the CC BY-4.0 License.
8    (See accompanying file LICENSE.txt or copy at
9 	https://creativecommons.org/licenses/by/4.0/
10 */
11 module hip.global.gamedef;
12 import hip.systems.game;
13 import hip.event.handlers.inputmap;
14 import hip.config.opts;
15 import hip.api.data.font;
16 import hip.api.data.image;
17 
18 //Default assets
19 struct HipDefaultAssets
20 {
21    private __gshared IImage _texture;
22    private __gshared HipFont _font;
23 
24    // static const(IImage) texture(){return cast(const)_texture;}
25    // static const(IHipFont) font(){return cast(const)_font;}
26 
27    static IImage texture(){return _texture;}
28    static HipFont font(){return _font;}
29 
30    immutable static string textureData = import(HIP_DEFAULT_TEXTURE);
31    immutable static string fontData = import(HIP_DEFAULT_FONT);
32 }
33 
34 
35 public:
36    enum ENGINE_NAME = "Hipreme Engine";
37    enum int SCREEN_WIDTH = 800;
38    enum int SCREEN_HEIGHT = 600;
39    ///Globally shared for accessing it on Android Game Thread
40    __gshared GameSystem sys;
41    __gshared HipInputMap map;
42 
43    __gshared float g_deltaTime = 0;
44 
45 
46 
47 
48 ///This function is callable only once.
49 bool loadDefaultAssets(void delegate() onSuccess, void delegate(string cause) onFailure)
50 {
51    import hip.font.ttf;
52    import hip.assets.image;
53    __gshared int succeededSteps = 0;
54    enum ASSETS_TO_LOAD = 2;
55 
56    if(succeededSteps > 0)
57       return false;
58    
59    import hip.console.log;
60 
61    hiplog("Loading default assets");
62    
63    auto image = new Image(HIP_DEFAULT_TEXTURE);
64    image.loadFromMemory(cast(ubyte[])HipDefaultAssets.textureData, (_)
65    {
66       hiplog("Loaded default image");
67       HipDefaultAssets._texture = image;
68       if(++succeededSteps == ASSETS_TO_LOAD)
69          onSuccess();
70    }, 
71    ()
72    {
73       onFailure("Failed loading default image.");
74    });
75 
76    hiplog("Loading default font");
77    auto font = new Hip_TTF_Font(HIP_DEFAULT_FONT, HIP_DEFAULT_FONT_SIZE);
78    if(!font.loadFromMemory(cast(ubyte[])HipDefaultAssets.fontData))
79       onFailure("Failed loading default font");
80    else
81    {
82       hiplog("Loaded default font");
83       HipDefaultAssets._font = font;
84       if(++succeededSteps == ASSETS_TO_LOAD)
85          onSuccess();
86    }
87 
88 
89    return true;
90 }
91 
92 export extern(System)
93 {
94    HipFont getDefaultFont()
95    {
96       return HipDefaultAssets.font;
97    }
98    /**
99    *  Use this instead of getDefaultFont.getFontWithSize, as it changes its internal state.
100    */
101    HipFont getDefaultFontWithSize(uint size)
102    {
103       return HipDefaultAssets._font.getFontWithSize(size);
104    }
105    IHipTexture getDefaultTexture()
106    {
107       __gshared IHipTexture texture;
108       if(texture is null)
109       {
110          import hip.assets.texture;
111          import hip.console.log;
112          logln("Image has loaded? ", HipDefaultAssets.texture.getName, HipDefaultAssets.texture.getWidth);
113          texture = new HipTexture(HipDefaultAssets.texture);
114       }
115       // return cast(const)texture;
116       return texture;
117    }
118 }